<template>
  <div>
    <el-card class="box-card">
      <el-form
        v-loading="loading"
        :model="predictForm"
        :rules="rules"
        ref="predictForm"
        label-width="120px"
        class="demo-predictForm"
      >
        <el-form-item label="选择模型" prop="modelId">
          <el-select v-model="predictForm.modelId" placeholder="请选择模型">
            <el-option
              v-for="item in modelList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上传样本文件" prop="filePath">
          <el-upload
            class="upload-demo"
            list-type="picture-card"
            :limit="1"
            :action="uploadUrl"
            :file-list="fileList"
            :on-success="handleFileSuccess"
            :on-error="handleFileError"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传样本文件，且不超过10MB
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="预测结果" prop="predictResult">
          <el-input
            type="textarea"
            :rows="6"
            v-model="predictForm.predictResult"
            disabled
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('predictForm')"
            >预测</el-button
          >
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { queryModelListByType } from "@/api/model";
import { exePredict } from "@/api/predict";
export default {
  name: "mnist",
  props: ['modelTypeId'],
  data() {
    return {
      loading: false,
      uploadUrl: process.env.VUE_APP_BASE_API + "/file/uploadSample",
      predictForm: {
        modelId: "",
        filePath: "",
        txt: "",
        predictResult: "",
      },
      rules: {
        modelId: [{ required: true, message: "请选择模型", trigger: "change" }],
        filePath: [
          { required: true, message: "请上传样本", trigger: "change" },
        ],
      },
      modelList: [],
      fileList: [],
    };
  },

  created() {
    this.queryModelList();
  },

  methods: {
    queryModelList() {
      queryModelListByType(this.modelTypeId)
        .then((response) => {
          console.log(response);
          this.modelList = response.data.records;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    handleFileSuccess(response, file, fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.predictForm.filePath = response.filePath;
    },
    handleFileError(err, file, fileList) {
      console.log(err);
      console.log(file);
      console.log(fileList);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          exePredict(this.predictForm)
            .then((response) => {
              this.loading = false;
              console.log(response);
              this.predictForm.predictResult = response.data.predictResult;
            })
            .catch(function (e) {
              this.loading = false;
              console.log(e);
            });
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.box-card {
  width: 600px;
}
</style>
